package com.hyacinth.cloudnote.modules.note.converter;

import com.hyacinth.cloudnote.common.converter.AbstractConverter;
import com.hyacinth.cloudnote.common.exception.ExceptionHolder;
import com.hyacinth.cloudnote.modules.note.dto.FavoriteDTO;
import com.hyacinth.cloudnote.modules.note.model.Favorite;
import com.hyacinth.cloudnote.modules.note.model.Note;
import com.hyacinth.cloudnote.modules.note.model.NoteBook;
import com.hyacinth.cloudnote.modules.note.model.Tag;
import com.hyacinth.cloudnote.modules.note.repository.FavoriteRepository;
import com.hyacinth.cloudnote.modules.note.repository.NoteBookRepository;
import com.hyacinth.cloudnote.modules.note.repository.NoteRepository;
import com.hyacinth.cloudnote.modules.note.repository.TagRepository;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

import static com.hyacinth.cloudnote.modules.common.errorcode.NoteErrorCode.E1000000001;

/**
 * Favorite实体与DTO之间接口数据转换实现
 * @author zhangfb
 */
@Component
public class FavoriteConverter extends AbstractConverter<Favorite, FavoriteDTO> {

    @Autowired
    private TagRepository tagRepository;
    @Autowired
    private NoteRepository noteRepository;
    @Autowired
    private NoteBookRepository noteBookRepository;
    @Autowired
    private FavoriteRepository favoriteRepository;

    @Override
    public Favorite toModel(@NonNull final FavoriteDTO dto) {
        if (dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public FavoriteDTO toDTO(@NonNull final Favorite model) {
        final FavoriteDTO dto = new FavoriteDTO();

        dto.setId(model.getId());
        dto.setNoteId(model.getNote() != null ? model.getNote().getId() : null);
        dto.setNoteTile(model.getNote() != null ? model.getNote().getTitle() : null);
        dto.setNoteBookId(model.getNoteBook() != null ? model.getNoteBook().getId() : null);
        dto.setNoteBookName(model.getNoteBook() != null ? model.getNoteBook().getName() : null);
        dto.setTagId(model.getTag() != null ? model.getTag().getId() : null);
        dto.setTagName(model.getTag() != null ? model.getTag().getName() : null);
        dto.setType(model.getType());

        this.convertCommonToDTO(model, dto);

        return dto;
    }

    /**
     * 构建新Model
     */
    private Favorite constructModel(final FavoriteDTO dto) {
        final Favorite model = new Favorite();

        this.dtoToModel(dto, model);
        return model;
    }

     /**
      * 更新Model
      */
    private Favorite updateModel(final FavoriteDTO dto) {
        final Favorite model = this.favoriteRepository.findById(dto.getId())
                        .orElseThrow(() -> ExceptionHolder.serviceException(E1000000001, "收藏夹-"+ dto.getId()));
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final FavoriteDTO dto, final Favorite model) {
        UUID noteBookId = dto.getNoteBookId();
        if (noteBookId != null) {
            NoteBook noteBook = noteBookRepository.findById(noteBookId).orElseThrow(() -> ExceptionHolder.serviceException(E1000000001, "笔记本-"+ noteBookId));
            model.setNoteBook(noteBook);
        }

        UUID noteId = dto.getNoteId();
        if (noteId != null) {
            Note note = noteRepository.findById(noteId).orElseThrow(() -> ExceptionHolder.serviceException(E1000000001, "笔记-"+ noteId));
            model.setNote(note);
        }

        UUID tagId = dto.getTagId();
        if (tagId != null) {
            Tag tag = tagRepository.findById(tagId).orElseThrow(() -> ExceptionHolder.serviceException(E1000000001, "标签-"+ tagId));
            model.setTag(tag);
        }
        model.setType(dto.getType());
    }
}
